package com.halflife.comeElectricity.basic.ProductType;

import com.halflife.framework.domain.Page;
import com.halflife.shiro.utils.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoyf on 2018/6/18.
 */
@Controller
@RequestMapping("/admin/productType/")
public class ProductTypeController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ProductTypeRepository productTypeRepository;

    @RequestMapping("list")
    public String gotoList(Map<String,Object> queryParam, Page page, HttpServletRequest request,
                           HttpServletResponse response, Model model) {
        // Merchant merchant  =  ShiroUtil.getMerchant();
        //推送分页Map参数
        if (page == null) {
            page = new Page();
        }
        if (queryParam == null) {
            queryParam = new HashMap<>();
        }
        page.setQueryParam(queryParam);
        page.setTotalCount(productTypeRepository.selectCountByPage(queryParam));
        List<ProductType> list;
        if(page.getTotalCount()>0){
            list = productTypeRepository.selectListByPage(queryParam);
        }else{
            list = new ArrayList<>();
        }
        model.addAttribute("list",list);
        model.addAttribute("queryParam", queryParam);
        page.setMode(model);
        return "productType/list";
    }
    @RequestMapping("operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                                        HttpServletRequest request, Model model) {
        if(id!=null) {
            ProductType productType = productTypeRepository.selectByPrimaryKey(id);
            model.addAttribute("productType",productType);
        }
        return "productType/operate";
    }
    @PostMapping("save")
    public String saveAdvertising(ProductType productType, HttpServletRequest request,
                                  HttpServletResponse response, Model model) {
        try {
            if(productType.getId()!=null){

                productTypeRepository.updateByPrimaryKeySelective(productType);
            }else {
                productType.init();
                productTypeRepository.insert(productType);
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        return this.gotoList(null, null,request, response, model);
    }
    @DeleteMapping("{id}")
    public ResponseEntity delete(@PathVariable Integer id, HttpServletRequest request){

        try {
            if(id!=null){
                this.productTypeRepository.deleteByPrimaryKey(id);
            }
            return new ResponseEntity(1, HttpStatus.OK);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // return Result.No("系统异常");
            return new ResponseEntity(1,HttpStatus.EXPECTATION_FAILED);
        }
    }
    @DeleteMapping("logicDel/{id}")
    public ResponseEntity logicDelete(@PathVariable Integer id, HttpServletRequest request){

        try {
            if(id!=null){
                this.productTypeRepository.selectByPrimaryKey(id);
            }
            return new ResponseEntity(1, HttpStatus.OK);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // return Result.No("系统异常");
            return new ResponseEntity(1,HttpStatus.EXPECTATION_FAILED);
        }
    }
}
